package com.kll.carolinehmibiz.presentation.presenter

import android.content.Context
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.base.net.NetCode
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.GetAssignCustomerServiceResponse
import com.kll.carolinehmibiz.net.bean.GetRoomIdResponse
import com.kll.carolinehmibiz.presentation.BasePresenter
import com.kll.carolinehmibiz.presentation.listener.MainContactServiceProtocol


/**
 * ====================================================
 * Created by Ziliang Cai on 2021/12/12 20:10
 * <a href="mailto:752777456@qq.com">Contact me</a>
 * ====================================================
 */
open class MainContactServicePresenter(context: Context, mView: MainContactServiceProtocol.IView): BasePresenter(context), MainContactServiceProtocol.IPresenter {

    private var mView: MainContactServiceProtocol.IView? = mView
    var getRoomIdIsBusying = false
    override fun getAssignCustomerService() {

        mView?.showAssignCSLoading()

        OperationManager.getInstancce().getAssignCustomerService(1, object: OperateListener{
            override fun onSuccess(state: Int, obj: Any?) {
                if(obj != null) {
                    mView?.assignCSSuccessed((obj as GetAssignCustomerServiceResponse).tencent_user_id)
                } else {
                    mView?.assignCSFailed(state, "分配客服失败，请重试！")
                }
            }

            override fun onFail(code: Int, message: String) {
                mView?.assignCSFailed(code, message)
            }
        })
    }

    override fun onCallVideo(accountNo: String) {

    }

    override fun onCallVoice(accountNo: String) {

    }

    override fun getRoomId(tencentUserId: String) {
        if (getRoomIdIsBusying) return
        getRoomIdIsBusying = true
        OperationManager.getInstancce().getRoomId(tencentUserId, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                getRoomIdIsBusying = false
                if (state == NetCode.NET_CODE_OK) {
                    mView?.getRoomIdSuccess(tencentUserId,(obj as GetRoomIdResponse).roomid)
                }
            }

            override fun onFail(code: Int, message: String) {
                getRoomIdIsBusying = false
                mView?.getRoomIdFailed(code, message)
            }

        })
    }
    override fun onDismiss() {
        super.onDismiss()
        mView = null
    }
}